<template>
    <el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible"
        class="SHZY-dialog SHZY-dialog_center" lock-scroll>
        <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
            <el-form-item label="员工姓名" prop="employeeFullName">
                <el-input v-model='dataForm.employeeFullName' placeholder='请输入' readonly clearable
                    :style='{ "width": "100%" }'>
                </el-input>
            </el-form-item>
            <el-form-item label="物料组" prop="materialgroupCode">
                <SHZY-select :list="materialGroupOptions" @change="change_matnrGroupCode" placeholder="请选择物料组"
                    v-model:bindValue="dataForm.materialgroupCode" isConcat label="name" value="code"></SHZY-select>
            </el-form-item>
            <el-form-item label="物料" prop="materialCode">
                <SHZY-select :list="materialOptions" placeholder="请选择物料" v-model:bindValue="dataForm.materialCode" isConcat
                    label="name" value="code" @change="handleMaterialChange(arguments)"></SHZY-select>
            </el-form-item>

            <el-form-item label="工序" prop="processIdList">
                <SHZY-TreeSelect v-model:bindValue="dataForm.processIdList" :options="modelingOptions"
                    @change="modelingChange(arguments)" placeholder="选择⼯序信息" :disabled="false" />
            </el-form-item>


            <el-form-item label="检验类型" prop="type">
                <el-select v-model="dataForm.type" placeholder='请选择' clearable>
                    <el-option v-for="(item, index) in checkTypeOptions" :key="index" :label="item.fullName"
                        :value="item.enCode"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="生产建模" prop="factoryId">
                <SHZY-TreeSelect v-model:bindValue="dataForm.factoryId" :options="treeData"  :defaultExpandAll="false"
                    @change="handleFactory(arguments)" placeholder="选择生产建模" :disabled="false" />
            </el-form-item>


            <el-form-item label="有效标志" prop="enabledMark">
                <el-switch v-model='dataForm.enabledMark' required active-value='1' inactive-value='0'>
                </el-switch>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取 消</el-button>
            <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { getArtrouteSelector } from '@/api/q1/business/ipqcInspection'
import factorystructureAPI from "@/api/q1/basedata/factorystructure";
import { addQmsbaseipqcinspector, getQmsbaseipqcinspector, saveQmsbaseipqcinspector } from '@/api/q1/processInspection/InspectorAllocation'
import mgRestAPI from "@/api/q1/basedata/materialGroup"
export default {
    data() {
        return {
            dataForm: {
                factoryId: '',
                processIdList: '',
                employeeFullName: '',
                employeeCode: '',
                materialgroupCode: '',
                materialCode: '',
                modelingId: '',
                type: '',
                eamId: '',
                enabledMark: '1',
                teamId: '',
                workshopId: '',
                workshopSectionId: '',
                stationId: '',
                artrouteId: ''
            },
            rules: {
                materialgroupCode: [
                    {
                        required: true,
                        message: '请选择物料组',
                        trigger: '"change"'
                    }
                ]
            },
            btnLoading: false,
            materialGroupOptions: [],
            materialOptions: [],
            modelingOptions: [],
            checkTypeOptions: [],
            treeData: [],
            visible: false
        }
    },
    props: {

    },
    created() {
        this.getDictionaryData()
    },
    methods: {
        init(employeeCode, employeeName, id) {
            this.dataForm = this.$options.data().dataForm
            this.visible = true
            this.$nextTick(() => {
                this.$refs['elForm'].resetFields();
                this.$set(this.dataForm, 'employeeCode', employeeCode)
                this.$set(this.dataForm, 'employeeFullName', employeeName)
                this.dataForm.id = id || 0;
            })
            if (id) {
                getQmsbaseipqcinspector(id).then(res => {
                    this.dataForm = res.data
                    if (this.dataForm.stationId != "") {
                        this.dataForm.factoryId = this.dataForm.stationId
                    } else if (this.dataForm.workshopSectionId != "") {
                        this.dataForm.factoryId = this.dataForm.workshopSectionId
                    } else if (this.dataForm.teamId != "") {
                        this.dataForm.factoryId = this.dataForm.teamId
                    } else {
                        this.dataForm.factoryId = this.dataForm.workshopId
                    }
                    const that = this
                    this.$parent.treeData.some(item => {
                        if (item.enCode == res.data.employeeCode) {
                            that.$set(that.dataForm, 'employeeCode', item.enCode)
                            that.$set(that.dataForm, 'employeeFullName', `${item.departmentName}\\${item.fullName}(${item.enCode})`)
                            return;
                        }
                    })
                    if (this.dataForm.modelingId) {
                        this.$set(this.dataForm, 'processIdList', this.dataForm.modelingId)
                    } else if (this.dataForm.artrouteId) {
                        this.$set(this.dataForm, 'processIdList', this.dataForm.artrouteId)
                    }
                    this.handleMaterialChange([this.dataForm.materialCode])
                    mgRestAPI.getListByMaterialGroup(this.dataForm.materialgroupCode).then(res => {
                        this.materialOptions = res.data.list;
                    });
                })
            }

        },
        dataFormSubmit() {
            this.$refs.elForm.validate(val => {
                if (val) {
                    this.btnLoading = true;
                    if (this.dataForm.id) {
                        saveQmsbaseipqcinspector(this.dataForm).then(res => {
                            this.$message({
                                message: res.msg,
                                type: 'success',
                                duration: 1000,
                                onClose: () => {
                                    this.btnLoading = false;
                                    this.visible = false;
                                    this.$emit('refresh', true)
                                }
                            });
                        })
                    } else {
                        addQmsbaseipqcinspector(this.dataForm).then(res => {
                            this.$message({
                                message: res.msg,
                                type: 'success',
                                duration: 1000,
                                onClose: () => {
                                    this.btnLoading = false;
                                    this.visible = false;
                                    this.$emit('refresh', true)
                                }
                            });
                        })
                    }

                }
            })

        },
        change_matnrGroupCode(val) {
            this.$set(this.dataForm, 'materialCode', '')
            this.materialOptions = [];
            if (!val) {
                return
            };
            mgRestAPI.getListByMaterialGroup(val).then(res => {
                this.materialOptions = res.data.list;
            });
        },
        modelingChange(data) {
            this.$set(this.dataForm, 'modelingId', '')
            this.$set(this.dataForm, 'artrouteId', '')
            if (data[1].children) {
                this.$set(this.dataForm, 'artrouteId', data[0])
            } else {
                this.$set(this.dataForm, 'modelingId', data[0])
                this.$set(this.dataForm, 'artrouteId', data[1].parentId)
            }
        },
        handleFactory(data) {
            this.$set(this.dataForm, 'teamId', '')
            this.$set(this.dataForm, 'workshopId', '')
            this.$set(this.dataForm, 'workshopSectionId', '')
            this.$set(this.dataForm, 'stationId', '')
            if (!data[1]) {
                return
            }
            //Workshop, Team, Section
            if (data[1].type == "Workshop") {
                this.$set(this.dataForm, 'workshopId', data[1].id)
            } else if (data[1].type == "Team") {
                this.$set(this.dataForm, 'teamId', data[1].id)
            } else if (data[1].type == "Section") {
                this.$set(this.dataForm, 'workshopSectionId', data[1].id)
            } else if (data[1].type == "Station") {
                this.$set(this.dataForm, 'stationId', data[1].id)
            }
			if (data[1].parentId != "")
				this.getFactoryInfo(data[1].parentId, this.treeData)
        },
		getFactoryInfo(parentId, data) {
			data.some(element => {
				if (element.id == parentId) {
					if (element.type == "Workshop") {
						this.dataForm.workshopId = element.id
					} else if (element.type == "Team") {
						this.dataForm.teamId = element.id
					} else if (element.type == "Section") {
						this.dataForm.workshopSectionId = element.id
					} else if (element.type == "Station") {
						this.dataForm.stationId = element.id
					}
					if (element.parentId != "") {
						this.getFactoryInfo(element.parentId, this.treeData)
					} else {
						return true
					}
				} else {
					if (element.hasChildren) {
						this.getFactoryInfo(parentId, element.children)
					}
				}
			})
		},
        handleMaterialChange(data) {
            getArtrouteSelector(data && data[0]).then(res => {
                this.modelingOptions = res.data.list
            })
        },
        getDictionaryData() {
            this.$store.dispatch('base/getDictionaryData', { sort: 'ipqcInspectionType' }).then((res) => {
                this.checkTypeOptions = res
            })
            factorystructureAPI.getDictionaryTypeSelector(0).then(res => {
                this.treeData = res.data.list
            })
            this.handleMaterialChange()
            mgRestAPI.getAll().then(res => {
                this.materialGroupOptions = res.data.list;
            });
        },
    }
}
</script>